package com.example.demo.module.repository.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.repository.MaterialPoJo;
import com.example.demo.entity.response.repository.MaterialResponse;
import com.example.demo.module.repository.service.MaterialService;
import com.example.demo.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 材料控制层.
 *
 * @author Mei Ruoxiao
 * @since 2020/9/15
 */
@Slf4j
@RestController("materialc")
@RequestMapping("/material")
@Api(tags = "材料接口")
public class MaterialController {
    @Resource
    private MaterialService service;

    /**
     * 获取材料列表
     *
     * @return 获取材料列表
     * @author Mei Ruoxiao
     * @since 2020/9/17
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取材料列表")
    public ResponseEntity<Rs<Page<MaterialResponse>>> page(
            @ApiParam("查询关键字") @RequestParam(value = "keywords", required = false) String keywords,
            @ApiParam(value = "材料类型:0-材料 1-成品 2-废品", example = "1") @RequestParam("type") int type,
            @ApiParam("仓库文件夹") @RequestParam(value = "repositoryId") final long repositoryId,
            @ApiParam(value = "页面", example = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1")
                    int page,
            @ApiParam(value = "页面大小", example = "10")
            @RequestParam(value = "size", required = false, defaultValue = "10")
                    int size,
            @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) long companyId) {
        log.info("page:获取材料列表[keywords:{}, type:{},page:{}, size:{}, companyId:{}]", keywords, type, page, size, companyId);

        Page<MaterialPoJo> poJoPage = service.page(keywords, type, repositoryId, page, size, companyId);
        Page<MaterialResponse> responsePage = CommonUtils.toPage(poJoPage, MaterialResponse::of);
        return Rs.ok(CommonUtils.getResponse(responsePage));
    }


    /**
     * 转仓（支持单个和批量）
     *
     * @author Mei Ruoxiao
     * @since 2020/9/17
     */
    @PutMapping("/move")
    @ApiOperation("转仓（支持单个和批量）")
    public ResponseEntity<Rs<Object>> move(
            @RequestParam(value = "ids") final List<Long> ids,
            @RequestParam(value = "repositoryId") final long repositoryId,
            @ApiParam(value = "转仓数量", example = "5") @RequestParam(value = "quantity") final int quantity,
            @ApiParam(value = "材料类型:0-材料 1-成品 2-废品", example = "1") @RequestParam("type") int type,
            @RequestParam(value = Constant.HeaderKey.COMPANY_ID) final long companyId) {
        log.info("move:转仓[ids:{}, stock:{},repositoryId:{}, type:{}, companyId:{}]", ids, repositoryId, quantity, type, companyId);
        return Rs.ok(service.move(ids, repositoryId, quantity, type, companyId));
    }
}
